package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Charging;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Page;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.StoreInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.CodeBitmapUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DensityUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.DrawableTextView;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.SimpleDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class PageAdapter extends RecyclerView.Adapter<PageAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<Page> mList;
    private SimpleDialog selfDialog;

    public PageAdapter(Context context){
        mContext = context;
        mList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Page.StoreInfoBean store_info = mList.get(position).getStore_info();
        //设置店名
        holder.name.setText(store_info.getName() == null ? "" :store_info.getName());
        //设置用户名
        holder.user.setText(store_info.getContact());



        //设置数字
        holder.num.setText(position+1+"");
        //设置电话
        holder.tel.setText(store_info.getTel());
        //判断设备类型
        if (mList.get(position).getDevice_type() == 4) {
            //密码设备
            holder.page_today.setVisibility(View.GONE);
            holder.page_off_line.setVisibility(View.VISIBLE);
            holder.icon.setImageResource(R.drawable.mimasuo);
         //   Glide.with(mContext).load(R.mipmap.xiaxianshijian).into(holder.page_time_icon);
            String s = mList.get(position).getLast_use_time() == null ? "未被使用" : mList.get(position).getLast_use_time();
            holder.time.setText("上次使用："+s);
            holder.start.setVisibility(View.INVISIBLE);
            int i = mList.get(position).getToday_use_num() + Integer.parseInt(mList.get(position).getTotal_charging_count());
            holder.page_off_line.setText("历史累计充电："+i+"次");

            holder.page_ll.setVisibility(View.GONE);
            holder.version.setText("总电量：15000mA");
            holder.day.setText("今日使用："+mList.get(position).getToday_use_num()+"次");
            if (mList.get(position).getToday_use_num() == 0) {
                holder.number.setText("今日耗电量： "+mList.get(position).getToday_use_num()+"mA");
            } else{
                holder.number.setText("今日耗电量： "+mList.get(position).getToday_use_num()+"000mA");
            }
            holder.detection.setVisibility(View.GONE);
        }else if (mList.get(position).getDevice_type() == 1) {
            //wifi设备
            holder.page_today.setVisibility(View.VISIBLE);
            holder.page_today.setText("今日使用："+mList.get(position).getToday_use_num()+"次");
            holder.page_off_line.setVisibility(View.VISIBLE);
            //设置电量百分比
            holder.page_ll.setVisibility(View.VISIBLE);
            int i = Integer.parseInt(mList.get(position).getDump_energy());
            holder.count.setText(i+"%");
            if (i > 90) {
                holder.md.setVisibility(View.VISIBLE);
                holder.pb.setVisibility(View.GONE);
            }else {
                holder.pb.setVisibility(View.VISIBLE);
                holder.pb.setProgress(i);
                holder.md.setVisibility(View.GONE);
            }

            if (mList.get(position).getOn_line().equals("1")) {
                //设置上线时间
                holder.time.setText("上线时间："+mList.get(position).getOnline_time());
                holder.icon.setImageResource(R.drawable.wifi);
                holder.detection.setVisibility(View.GONE);
               // Glide.with(mContext).load(R.mipmap.shangxianshijian).into(holder.page_time_icon);
                holder.page_off_line.setVisibility(View.GONE);

            }else {
                holder.detection.setVisibility(View.INVISIBLE);
                holder.icon.setImageResource(R.drawable.wifitwo);
                //设置上线时间
                if (mList.get(position).getOffline_time() != null) {
                    holder.time.setText("离线时间："+mList.get(position).getOffline_time());
                  //  Glide.with(mContext).load(R.mipmap.xiaxianshijian).into(holder.page_time_icon);
                }else {
                    holder.time.setText("该设备未被使用");
                }


                //离线原因
                holder.page_off_line.setVisibility(View.VISIBLE);
                if (mList.get(position).getOffline_message_status().equals("0")) {
                    holder.page_off_line.setText(mContext.getString(R.string.off_line_cdb)+"未联网或未开机");
                }else {
                    if (Integer.parseInt(mList.get(position).getDump_energy()) < 10) {
                        holder.page_off_line.setText(mContext.getString(R.string.off_line_cdb)+"设备电量不足10%已自动关机");
                    }else {
                        holder.page_off_line.setText(mContext.getString(R.string.off_line_cdb)+"设备已关机");
                    }
                }
            }
            holder.start.setVisibility(View.VISIBLE);
            if (mList.get(position).getGreen_light_status().equals("0")) {
                holder.start.setImageResource(R.mipmap.guanbi);
            }else if (mList.get(position).getGreen_light_status().equals("1")) {
                Glide.with(mContext).load(R.drawable.shanshuo).into(holder.start);
            }else {
                holder.start.setImageResource(R.mipmap.changliang);
            }
            //设置充电次数
            holder.number.setVisibility(View.VISIBLE);
            holder.day.setVisibility(View.VISIBLE);
            holder.version.setVisibility(View.VISIBLE);
            int a = mList.get(position).getToday_use_num() + Integer.parseInt(mList.get(position).getTotal_charging_count());

            holder.number.setText("历史累计充电："+a+"次");
            //设置在线天数
            holder.day.setText(mContext.getString(R.string.to_day)+mList.get(position).getTotal_online_count());
            //设置固件版本
            if (mList.get(position).getFirmware_version() == null) {
                holder.version.setText(mContext.getString(R.string.cdb_version)+"未获取");
            }else {
                holder.version.setText(mContext.getString(R.string.cdb_version)+ (String)mList.get(position).getFirmware_version());
            }
        }else if (mList.get(position).getDevice_type() == 5) {
            if (mList.get(position).getOn_line().equals("1")) {
                holder.icon.setImageResource(R.drawable.zjj);
            }else{
                holder.icon.setImageResource(R.drawable.zjjlx);
            }
            //纸巾机
            int i = Integer.parseInt(mList.get(position).getDump_energy());

            holder.start.setVisibility(View.GONE);
            holder.detection.setVisibility(View.GONE);
            holder.page_ll.setVisibility(View.GONE);
            holder.page_off_line.setVisibility(View.GONE);
            int i1 = Integer.parseInt(mList.get(position).getTotal_charging_count()) + mList.get(position).getToday_use_num();
            holder.version.setText("累计领纸:"+i1+"次");
            holder.day.setText("今日领纸："+mList.get(position).getToday_use_num()+"次");
            holder.number.setText("剩余纸巾:"+ i+"次");
            String s = mList.get(position).getLast_use_time() == null ? "未被使用" : mList.get(position).getLast_use_time();
            holder.time.setText("上次使用："+s);
        }
        //今日涨粉
        holder.page_zhang.setText(mList.get(position).getSub_count()+" (今日)");
        //设置设备编号
        holder.id.setText(mContext.getString(R.string.cdb_id)+ mList.get(position).getAlias());

        //设置地址
        holder.location.setText(store_info.getArea_name()+store_info.getSpecific_address());
        holder.detection.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(mList.get(position),position);
        });
        holder.tel.setOnClickListener(v -> {
            Dialog(position);

        });

        holder.page_er.setOnClickListener(v -> {
            Bitmap bitmap;
            if (mList.get(position).getDevice_type() == 4) {
                //密码设备
                if (mList.get(position).getPwd_type().equals("3")) {
                    bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.mima_url3)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
                }else if (mList.get(position).getPwd_type().equals("2")) {
                    bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.mima_url2)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
                }else{
                    bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.mima_url)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
                }
            }else {
                //wifi设备
                bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.wifi_url)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
            }
            DialogUtils.TSImageDialog(mContext,store_info.getName(),mList.get(position).getAlias(),store_info.getImg_url(),bitmap);
        });
    }

    private void Dialog(int posotion) {
        selfDialog = new SimpleDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage("是否拨打该号码："+mList.get(posotion).getStore_info().getTel());
        selfDialog.setYesOnclickListener("呼叫", () -> {
            //用intent启动拨打电话
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mList.get(posotion).getStore_info().getTel()));
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mContext.startActivity(intent);
            selfDialog.dismiss();
        });
        selfDialog.setNoOnclickListener("取消", () -> {
            selfDialog.dismiss();
        });
        selfDialog.show();
    }



    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        TextView time,num,number,name,day,version,count,user,tel,location,id,detection,page_off_line,page_today;
        ImageView icon,start,md,page_time_icon,page_er;
        DrawableTextView page_zhang;
        ProgressBar pb;
        LinearLayout page_ll;
        public ViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.page_time);
            page_today = (TextView) itemView.findViewById(R.id.page_today);
            num = (TextView) itemView.findViewById(R.id.page_info_num);
            number = (TextView) itemView.findViewById(R.id.page_num);
            day = (TextView) itemView.findViewById(R.id.page_day);
            name = (TextView) itemView.findViewById(R.id.page_name);
            version = (TextView) itemView.findViewById(R.id.page_version);
            count = (TextView) itemView.findViewById(R.id.page_dianliang);
            user = (TextView) itemView.findViewById(R.id.page_user);
            detection = (TextView) itemView.findViewById(R.id.page_detection);
            icon = (ImageView) itemView.findViewById(R.id.page_info_icon);
            start = (ImageView) itemView.findViewById(R.id.page_info_start);
            md = (ImageView) itemView.findViewById(R.id.page_md);
            page_time_icon = (ImageView) itemView.findViewById(R.id.page_time_icon);
            page_er = (ImageView) itemView.findViewById(R.id.page_er);
            tel = (TextView) itemView.findViewById(R.id.page_tel);
            location = (TextView) itemView.findViewById(R.id.page_location);
            id = (TextView) itemView.findViewById(R.id.page_id);
            page_off_line = (TextView) itemView.findViewById(R.id.page_off_line);
            pb = (ProgressBar) itemView.findViewById(R.id.page_pb);
            page_ll = (LinearLayout) itemView.findViewById(R.id.page_ll);
            page_zhang = (DrawableTextView) itemView.findViewById(R.id.page_zhang);

        }
    }

    public void addData(List<Page> data){
        if (mList != null) {
            mList.clear();
            mList.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void addLastData(List<Page> data){
        mList.addAll(data);
        notifyDataSetChanged();
    }

    public interface OnItemClickListenter {
        void itemClickListenter(Page dataBean, int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }
}
